package com.xy.mainp.main.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xy.mainp.FragmentHomeBinding;
import com.xy.mainp.R;
import com.xy.mainp.base.TittleBarFragmnet;
import com.xy.mainp.home.fragment.AllFragment;
import com.xy.mainp.home.fragment.AndroidFragment;
import com.xy.mainp.home.fragment.BenifitFragment;
import com.xy.mainp.home.fragment.ExtendResFragment;
import com.xy.mainp.home.fragment.FrontFragment;
import com.xy.mainp.home.fragment.IOSFragment;
import com.xy.mainp.home.fragment.TodayFragment;
import com.xy.mainp.home.fragment.VideoFragment;

import java.util.ArrayList;

/**
 * Created by John on 2016/10/28.
 */

public class  HomeFragment extends TittleBarFragmnet {

    private FragmentHomeBinding binding;
    private ArrayList<Fragment> fragments;
    private LayoutInflater inflater;
    private String[] tittles;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = LayoutInflater.from(getContext());
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home,null,false);
        setContentView(binding.getRoot());

        initData();
        initView();
    }

    private void initView() {
        binding.vpHome.setAdapter(new HomeVPAdapter(getFragmentManager()));
        binding.tlHome.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tlHome.setupWithViewPager(binding.vpHome);
    }

    private void initData() {
        fragments = new ArrayList<Fragment>();
        fragments.add(new TodayFragment());
        fragments.add(new AndroidFragment());
        fragments.add(new IOSFragment());
        fragments.add(new BenifitFragment());
        fragments.add(new VideoFragment());
        fragments.add(new ExtendResFragment());
        fragments.add(new FrontFragment());
        fragments.add(new AllFragment());
        tittles = getResources().getStringArray(R.array.home_category);
    }


    class HomeVPAdapter extends FragmentStatePagerAdapter{

        public HomeVPAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size() != 0 ? fragments.size() : 0;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tittles[position];
        }
    }
}
